package com.lagou.edu.controller;

import com.lagou.edu.pojo.ApiResponse;
import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.List;

/**
 * @author leonzhangxf
 */
@Controller
@RequestMapping("/resume")
public class ResumeController {

    private final ResumeService resumeService;

    public ResumeController(ResumeService resumeService) {
        this.resumeService = resumeService;
    }

    @GetMapping("list-page")
    public String resumeListPage() {
        return "list";
    }

    @GetMapping("/list")
    @ResponseBody
    public ApiResponse<List<Resume>> list() {
        return ApiResponse.newSuccess(resumeService.list());
    }

    @PostMapping
    @ResponseBody
    public ApiResponse<Void> createResume(@RequestBody Resume resume) {
        resumeService.create(resume);
        return ApiResponse.newSuccess(null);
    }

    @PutMapping("{id}")
    @ResponseBody
    public ApiResponse<Void> updateResume(@PathVariable("id") Long id, @RequestBody Resume resume) {
        resume.setId(id);
        resumeService.create(resume);
        return ApiResponse.newSuccess(null);
    }

    @DeleteMapping("{id}")
    @ResponseBody
    public ApiResponse<Void> deleteResume(@PathVariable("id") Long id) {
        resumeService.delete(id);
        return ApiResponse.newSuccess(null);
    }

    @ExceptionHandler(Exception.class)
    public ApiResponse<Void> handleException(Exception ex) {
        return ApiResponse.newErr(0, ex.getMessage());
    }
}
